﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YQGL.Common;

namespace YQGL.Controllers
{
    public class QYBBController : EnterpriseBaseController
    {
        //
        // GET: /QYBB/
        BLL.DM_QYDL DLBLL = new BLL.DM_QYDL();
        BLL.DM_QYLX LXBLL = new BLL.DM_QYLX();
        BLL.DJ_QYZCXX DJ_QYZCXXBLL = new BLL.DJ_QYZCXX();

        BLL.SB_QYBB SBQYBBBLL = new BLL.SB_QYBB();

        BLL.DM_QYBB DMQYBBBLL = new BLL.DM_QYBB();

        BLL.V_SB_YSBXX V_SB_YSBXXBLL = new BLL.V_SB_YSBXX();
        BLL.Dm_ND Dm_NDBLL = new BLL.Dm_ND();

        public ActionResult Index(string Year)
        {
            string LoginID = OperateSession.get("LoginID");
            if (string.IsNullOrEmpty(LoginID))
            {

                Response.Redirect(Url.Action("Index", "Index"));
            }
            else
            {

                Model.DJ_QYZCXX infoModel = new Model.DJ_QYZCXX();
                ViewData["LoginID"] = LoginID;
                infoModel = DJ_QYZCXXBLL.GetModel(LoginID);

                if (infoModel == null)
                {

                    Response.Write("企业基本信息未上报");
                    Response.Flush();
                    Response.End();
                    return null;

                }
                else
                {
                    if (infoModel.ZTDM != "2")
                    {

                        Response.Write("企业基本信息未通过审核,待通过审核时才能填写企业报表");
                        Response.Flush();
                        Response.End();
                        return null;
                    }
                    List<SelectListItem> YearList = Dm_NDBLL.GetModelList("").Select(h => new SelectListItem() { Text = h.SSND.Trim(), Value = h.SSND.Trim() }).ToList();
                    if (string.IsNullOrEmpty(Year))
                    {

                        YearList.ForEach(h => h.Selected = (h.Value.ToString().Trim() == DateTime.Now.Year.ToString()));
                    }
                    else
                    {

                        YearList.ForEach(h => h.Selected = (h.Value.ToString().Trim() == Year.ToString().Trim()));

                    }
                    List<Model.V_SB_YSBXX> V_SB_YSBXXList = V_SB_YSBXXBLL.GetModelList("QYBM='" + LoginID + "'  and SSND=" + YearList.FirstOrDefault(h => h.Selected).Value.Trim());

                    if (V_SB_YSBXXList.Count == 0)
                    {
                        Response.Write("无法获取企业报表格式,可能原因未填写企业分类,请联系园区管理员");
                        Response.Flush();
                        Response.End();
                        return null;
                    }
                    ViewData["Nsrmc"] = infoModel.Nsrmc;
                    ViewData["DLMC"] = DLBLL.GetModel(infoModel.QYDL_DM).qydl_mc;
                    Model.DM_QYLX LXModel = LXBLL.GetModel(infoModel.QYLX_DM);
                    if (LXModel != null)
                    {

                        ViewData["LXMC"] = LXModel.QYLX_MC;
                    }


                  
                   

                    List<Model.DM_QYBB> DMQYBBList = DMQYBBBLL.GetModelList("");
                    foreach (Model.V_SB_YSBXX item in V_SB_YSBXXList)
                    {
                        DateTime LastDt = new DateTime(int.Parse(item.SSND), int.Parse(item.SSYF), 1);
                        List<Model.DM_QYBB> YBQYBBList = DMQYBBList.Where(h => (h.DBYF.Contains(LastDt.Month.ToString().PadLeft(2, '0')) | string.IsNullOrEmpty(h.DBYF)) & h.BBlxdm == "1" & (string.IsNullOrEmpty(h.QYDL_DM) | ((h.QYDL_DM == infoModel.QYDL_DM & string.IsNullOrEmpty(h.Qylx_dm)) | (!string.IsNullOrEmpty(h.QYDL_DM) & !string.IsNullOrEmpty(h.Qylx_dm) & h.QYDL_DM == infoModel.QYDL_DM & h.Qylx_dm == infoModel.QYLX_DM)))).ToList();

                        List<Model.DM_QYBB> JBQYBBList = DMQYBBList.Where(h => (h.DBYF.Contains(LastDt.Month.ToString().PadLeft(2, '0')) | string.IsNullOrEmpty(h.DBYF)) & h.BBlxdm == "2" & (string.IsNullOrEmpty(h.QYDL_DM) | ((h.QYDL_DM == infoModel.QYDL_DM & string.IsNullOrEmpty(h.Qylx_dm)) | (!string.IsNullOrEmpty(h.QYDL_DM) & !string.IsNullOrEmpty(h.Qylx_dm) & h.QYDL_DM == infoModel.QYDL_DM & h.Qylx_dm == infoModel.QYLX_DM)))).ToList();


                        List<Model.DM_QYBB> NBQYBBList = DMQYBBList.Where(h => (h.DBYF.Contains(LastDt.Month.ToString().PadLeft(2, '0')) | string.IsNullOrEmpty(h.DBYF)) & h.BBlxdm == "3" & (string.IsNullOrEmpty(h.QYDL_DM) | ((h.QYDL_DM == infoModel.QYDL_DM & string.IsNullOrEmpty(h.Qylx_dm)) | (!string.IsNullOrEmpty(h.QYDL_DM) & !string.IsNullOrEmpty(h.Qylx_dm) & h.QYDL_DM == infoModel.QYDL_DM & h.Qylx_dm == infoModel.QYLX_DM)))).ToList();

                        List<Model.DM_QYBB> FJQYBBList = DMQYBBList.Where(h => (h.DBYF.Contains(LastDt.Month.ToString().PadLeft(2, '0')) | string.IsNullOrEmpty(h.DBYF)) & h.BBlxdm == "4" & (string.IsNullOrEmpty(h.QYDL_DM) | ((h.QYDL_DM == infoModel.QYDL_DM & string.IsNullOrEmpty(h.Qylx_dm)) | (!string.IsNullOrEmpty(h.QYDL_DM) & !string.IsNullOrEmpty(h.Qylx_dm) & h.QYDL_DM == infoModel.QYDL_DM & h.Qylx_dm == infoModel.QYLX_DM)))).ToList();
                        item.QYBBInfo = ((YBQYBBList.Count > 0 ? "月报," : "") + (JBQYBBList.Count > 0 ? "季报," : "") + (NBQYBBList.Count > 0 ? "年报," : "") + (FJQYBBList.Count > 0 ? "附加报表," : "")).TrimEnd(',');
                    }

                    ViewData["YearList"] = YearList;
                    return View("Index", V_SB_YSBXXList);
                }
            }
            return View();
        }
        public ActionResult QYBBLook(string Year, string Month)
        {

            return QYBBData("QYBBLook", Year, Month);
        }

        private ActionResult QYBBData(string ViewName, string Year, string Month)
        {

            List<Model.SB_QYBB> SBQYBBList = new List<Model.SB_QYBB>();
            Model.DJ_QYZCXX infoModel = new Model.DJ_QYZCXX();
            string LoginID = OperateSession.get("LoginID");
            if (string.IsNullOrEmpty(LoginID))
            {

                Response.Redirect(Url.Action("Index", "Index"));
            }
            else
            {

                ViewData["LoginID"] = LoginID;
                infoModel = DJ_QYZCXXBLL.GetModel(LoginID);

                if (infoModel == null)
                {

                    Response.Write("企业基本信息未上报");
                    Response.Flush();
                    Response.End();
                    return null;

                }
                else
                {
                    if (infoModel.ZTDM != "2")
                    {

                        Response.Write("企业基本信息未通过审核");
                        Response.Flush();
                        Response.End();
                        return null;
                    }
                    ViewData["Nsrmc"] = infoModel.Nsrmc;
                    ViewData["DLMC"] = DLBLL.GetModel(infoModel.QYDL_DM).qydl_mc;
                    Model.DM_QYLX LXModel = LXBLL.GetModel(infoModel.QYLX_DM);
                    if (LXModel != null)
                    {

                        ViewData["LXMC"] = LXModel.QYLX_MC;
                    }

                }
            }

            List<SelectListItem> MonthList = new List<SelectListItem>();

            DateTime LastDt = new DateTime(int.Parse(Year), int.Parse(Month), 1);

            for (int i = 1; i < 13; i++)
            {
                MonthList.Add(new SelectListItem() { Text = (LastDt.Year - 1) + "年" + i + "月", Value = (LastDt.Year - 1).ToString() + "-" + i.ToString().PadLeft(2, '0') });
            }

            for (int i = 1; i < LastDt.Month + 1; i++)
            {

                if (i == LastDt.Month)
                {
                    MonthList.Add(new SelectListItem() { Text = (LastDt.Year) + "年" + i + "月", Value = (LastDt.Year).ToString() + "-" + i.ToString().PadLeft(2, '0'), Selected = true });
                }
                else
                {
                    MonthList.Add(new SelectListItem() { Text = (LastDt.Year) + "年" + i + "月", Value = (LastDt.Year).ToString() + "-" + i.ToString().PadLeft(2, '0') });
                }
            }
            ViewData["MonthList"] = MonthList;
            ViewData["YearAndMonth"] = (LastDt.Year).ToString() + "-" + LastDt.Month.ToString().PadLeft(2, '0');
            List<Model.DM_QYBB> DMQYBBList = DMQYBBBLL.GetModelList("");
            List<Model.DM_QYBB> YBQYBBList = DMQYBBList.Where(h => (h.DBYF.Contains(LastDt.Month.ToString().PadLeft(2, '0')) | string.IsNullOrEmpty(h.DBYF)) & h.BBlxdm == "1" & (string.IsNullOrEmpty(h.QYDL_DM) | ((h.QYDL_DM == infoModel.QYDL_DM & string.IsNullOrEmpty(h.Qylx_dm)) | (!string.IsNullOrEmpty(h.QYDL_DM) & !string.IsNullOrEmpty(h.Qylx_dm) & h.QYDL_DM == infoModel.QYDL_DM & h.Qylx_dm == infoModel.QYLX_DM)))).GroupBy(h => new { BBXM_DM = h.BBXM_DM, BBXM_MC = h.BBXM_MC }).Select(h => new Model.DM_QYBB { BBXM_DM = h.Key.BBXM_DM, BBXM_MC = h.Key.BBXM_MC }).ToList();

            List<Model.DM_QYBB> JBQYBBList = DMQYBBList.Where(h => (h.DBYF.Contains(LastDt.Month.ToString().PadLeft(2, '0')) | string.IsNullOrEmpty(h.DBYF)) & h.BBlxdm == "2" & (string.IsNullOrEmpty(h.QYDL_DM) | ((h.QYDL_DM == infoModel.QYDL_DM & string.IsNullOrEmpty(h.Qylx_dm)) | (!string.IsNullOrEmpty(h.QYDL_DM) & !string.IsNullOrEmpty(h.Qylx_dm) & h.QYDL_DM == infoModel.QYDL_DM & h.Qylx_dm == infoModel.QYLX_DM)))).GroupBy(h => new { BBXM_DM = h.BBXM_DM, BBXM_MC = h.BBXM_MC }).Select(h => new Model.DM_QYBB { BBXM_DM = h.Key.BBXM_DM, BBXM_MC = h.Key.BBXM_MC }).ToList();


            List<Model.DM_QYBB> NBQYBBList = DMQYBBList.Where(h => (h.DBYF.Contains(LastDt.Month.ToString().PadLeft(2, '0')) | string.IsNullOrEmpty(h.DBYF)) & h.BBlxdm == "3" & (string.IsNullOrEmpty(h.QYDL_DM) | ((h.QYDL_DM == infoModel.QYDL_DM & string.IsNullOrEmpty(h.Qylx_dm)) | (!string.IsNullOrEmpty(h.QYDL_DM) & !string.IsNullOrEmpty(h.Qylx_dm) & h.QYDL_DM == infoModel.QYDL_DM & h.Qylx_dm == infoModel.QYLX_DM)))).GroupBy(h => new { BBXM_DM = h.BBXM_DM, BBXM_MC = h.BBXM_MC }).Select(h => new Model.DM_QYBB { BBXM_DM = h.Key.BBXM_DM, BBXM_MC = h.Key.BBXM_MC }).ToList();

            List<Model.DM_QYBB> FJQYBBList = DMQYBBList.Where(h => (h.DBYF.Contains(LastDt.Month.ToString().PadLeft(2, '0')) | string.IsNullOrEmpty(h.DBYF)) & h.BBlxdm == "4" & (string.IsNullOrEmpty(h.QYDL_DM) | ((h.QYDL_DM == infoModel.QYDL_DM & string.IsNullOrEmpty(h.Qylx_dm)) | (!string.IsNullOrEmpty(h.QYDL_DM) & !string.IsNullOrEmpty(h.Qylx_dm) & h.QYDL_DM == infoModel.QYDL_DM & h.Qylx_dm == infoModel.QYLX_DM)))).GroupBy(h => new { BBXM_DM = h.BBXM_DM, BBXM_MC = h.BBXM_MC }).Select(h => new Model.DM_QYBB { BBXM_DM = h.Key.BBXM_DM, BBXM_MC = h.Key.BBXM_MC }).ToList();

            //List<Model.DM_QYBB> DMQYBBListNew = new List<Model.DM_QYBB>();
            //DMQYBBListNew.AddRange(YBQYBBList);
            //DMQYBBListNew.AddRange(JBQYBBList);
            //DMQYBBListNew.AddRange(NBQYBBList);
            //DMQYBBListNew.AddRange(FJQYBBList);
            //foreach (Model.DM_QYBB item in DMQYBBListNew)
            //{
            //    item.SSSQ_Q = DateTime.Parse(GetStartAndEndTime(LastDt, item.BBlxdm).Split('%')[0]);
            //    item.SSSQ_Z = DateTime.Parse(GetStartAndEndTime(LastDt, item.BBlxdm).Split('%')[1]);

            //}
            SBQYBBList = SBQYBBBLL.GetModelList("QYBM='" + LoginID + "' and SSND='" + (LastDt.Year).ToString() + "' and SSYF='" + LastDt.Month.ToString().PadLeft(2, '0') + "'");
            ViewData["SBQYBBList"] = SBQYBBList;
            ViewData["YBQYBBList"] = YBQYBBList;
            ViewData["JBQYBBList"] = JBQYBBList;
            ViewData["NBQYBBList"] = NBQYBBList;
            ViewData["FJQYBBList"] = FJQYBBList;
            if (SBQYBBList.Count == 0)
            {
                string actionOP = BaseStruct.ActionOperationStruct.ADD_NEXT;
                ViewData["hdMethod"] = actionOP.ToString();
            }
            else
            {
                string actionOP = BaseStruct.ActionOperationStruct.EDIT;
                ViewData["hdMethod"] = actionOP.ToString();
            }
            return View(ViewName, LastDt);

        }
        public ActionResult QYBBPUb(string Year, string Month)
        {

            return QYBBData("QYBBPUb", Year, Month);
        }




        [HttpPost]
        public ActionResult QYBBPartial(string YM)
        {
            try
            {
                DateTime LastDt = DateTime.Parse(YM + "-01");
                Model.DJ_QYZCXX infoModel = new Model.DJ_QYZCXX();

                string LoginID = OperateSession.get("LoginID");
                if (string.IsNullOrEmpty(LoginID))
                {

                    Response.Write("Failed:未登录");
                    Response.Flush();
                    Response.End();
                    return null;
                }
                else
                {

                    ViewData["LoginID"] = LoginID;
                    infoModel = DJ_QYZCXXBLL.GetModel(LoginID);

                    if (infoModel == null)
                    {

                        Response.Write("Failed:企业基本信息未上报");
                        Response.Flush();
                        Response.End();
                        return null;

                    }
                    else
                    {
                        ViewData["Nsrmc"] = infoModel.Nsrmc;
                        ViewData["DLMC"] = DLBLL.GetModel(infoModel.QYDL_DM).qydl_mc;
                        Model.DM_QYLX LXModel = LXBLL.GetModel(infoModel.QYLX_DM);
                        if (LXModel != null)
                        {

                            ViewData["LXMC"] = LXModel.QYLX_MC;
                        }

                    }
                }

                List<Model.DM_QYBB> DMQYBBList = DMQYBBBLL.GetModelList("");
                List<Model.DM_QYBB> YBQYBBList = DMQYBBList.Where(h => (h.DBYF.Contains(LastDt.Month.ToString().PadLeft(2, '0')) | string.IsNullOrEmpty(h.DBYF)) & h.BBlxdm == "1" & (string.IsNullOrEmpty(h.QYDL_DM) | ((h.QYDL_DM == infoModel.QYDL_DM & string.IsNullOrEmpty(h.Qylx_dm)) | (!string.IsNullOrEmpty(h.QYDL_DM) & !string.IsNullOrEmpty(h.Qylx_dm) & h.QYDL_DM == infoModel.QYDL_DM & h.Qylx_dm == infoModel.QYLX_DM)))).GroupBy(h => new { BBXM_DM = h.BBXM_DM, BBXM_MC = h.BBXM_MC }).Select(h => new Model.DM_QYBB { BBXM_DM = h.Key.BBXM_DM, BBXM_MC = h.Key.BBXM_MC }).ToList();

                List<Model.DM_QYBB> JBQYBBList = DMQYBBList.Where(h => (h.DBYF.Contains(LastDt.Month.ToString().PadLeft(2, '0')) | string.IsNullOrEmpty(h.DBYF)) & h.BBlxdm == "2" & (string.IsNullOrEmpty(h.QYDL_DM) | ((h.QYDL_DM == infoModel.QYDL_DM & string.IsNullOrEmpty(h.Qylx_dm)) | (!string.IsNullOrEmpty(h.QYDL_DM) & !string.IsNullOrEmpty(h.Qylx_dm) & h.QYDL_DM == infoModel.QYDL_DM & h.Qylx_dm == infoModel.QYLX_DM)))).GroupBy(h => new { BBXM_DM = h.BBXM_DM, BBXM_MC = h.BBXM_MC }).Select(h => new Model.DM_QYBB { BBXM_DM = h.Key.BBXM_DM, BBXM_MC = h.Key.BBXM_MC }).ToList();


                List<Model.DM_QYBB> NBQYBBList = DMQYBBList.Where(h => (h.DBYF.Contains(LastDt.Month.ToString().PadLeft(2, '0')) | string.IsNullOrEmpty(h.DBYF)) & h.BBlxdm == "3" & (string.IsNullOrEmpty(h.QYDL_DM) | ((h.QYDL_DM == infoModel.QYDL_DM & string.IsNullOrEmpty(h.Qylx_dm)) | (!string.IsNullOrEmpty(h.QYDL_DM) & !string.IsNullOrEmpty(h.Qylx_dm) & h.QYDL_DM == infoModel.QYDL_DM & h.Qylx_dm == infoModel.QYLX_DM)))).GroupBy(h => new { BBXM_DM = h.BBXM_DM, BBXM_MC = h.BBXM_MC }).Select(h => new Model.DM_QYBB { BBXM_DM = h.Key.BBXM_DM, BBXM_MC = h.Key.BBXM_MC }).ToList();

                List<Model.DM_QYBB> FJQYBBList = DMQYBBList.Where(h => (h.DBYF.Contains(LastDt.Month.ToString().PadLeft(2, '0')) | string.IsNullOrEmpty(h.DBYF)) & h.BBlxdm == "4" & (string.IsNullOrEmpty(h.QYDL_DM) | ((h.QYDL_DM == infoModel.QYDL_DM & string.IsNullOrEmpty(h.Qylx_dm)) | (!string.IsNullOrEmpty(h.QYDL_DM) & !string.IsNullOrEmpty(h.Qylx_dm) & h.QYDL_DM == infoModel.QYDL_DM & h.Qylx_dm == infoModel.QYLX_DM)))).GroupBy(h => new { BBXM_DM = h.BBXM_DM, BBXM_MC = h.BBXM_MC }).Select(h => new Model.DM_QYBB { BBXM_DM = h.Key.BBXM_DM, BBXM_MC = h.Key.BBXM_MC }).ToList();

                ViewData["YBQYBBList"] = YBQYBBList;
                ViewData["JBQYBBList"] = JBQYBBList;
                ViewData["NBQYBBList"] = NBQYBBList;
                ViewData["FJQYBBList"] = FJQYBBList;
                List<Model.SB_QYBB> SBQYBBList = SBQYBBBLL.GetModelList("QYBM='" + LoginID + "' and SSND='" + (LastDt.Year).ToString() + "' and SSYF='" + LastDt.Month.ToString().PadLeft(2, '0') + "'");
                ViewData["SBQYBBList"] = SBQYBBList;

                if (SBQYBBList.Count == 0)
                {
                    string actionOP = BaseStruct.ActionOperationStruct.ADD_NEXT;
                    ViewData["hdMethod"] = actionOP.ToString();
                }
                else
                {
                    string actionOP = BaseStruct.ActionOperationStruct.EDIT;
                    ViewData["hdMethod"] = actionOP.ToString();
                }

                return PartialView("QYBBPartial", LastDt);
            }
            catch (System.Exception ex)
            {
                Response.Write("Failed:" + ex.Message);
                Response.Flush();
                Response.End();
                return null;
            }

            return null;
        }

        public string Save()
        {

            List<Model.SB_QYBB> SBQYBBList = new List<Model.SB_QYBB>();
            bool isSuccess = true;
            string errorMessage = "";
            string YearAndMonth = Fetch.Post("hdYearAndMonth");
            DateTime LastDt = DateTime.Parse(YearAndMonth + "-01");

            string LoginID = OperateSession.get("LoginID");
            if (string.IsNullOrEmpty(LoginID))
            {
                isSuccess = false;
                errorMessage = "未登录";
            }
            List<Model.SB_QYBB> SBQYBBListOld = SBQYBBBLL.GetModelList("QYBM='" + LoginID + "' and SSND='" + (LastDt.Year).ToString() + "' and SSYF='" + LastDt.Month.ToString().PadLeft(2, '0') + "'");

            Model.DJ_QYZCXX infoModel = DJ_QYZCXXBLL.GetModel(LoginID);

            if (infoModel == null)
            {
                isSuccess = false;
                errorMessage = "企业基本信息未上报";


            }
            List<Model.DM_QYBB> DMQYBBList = DMQYBBBLL.GetModelList("");
            List<Model.DM_QYBB> YBQYBBList = DMQYBBList.Where(h => (h.DBYF.Contains(LastDt.Month.ToString().PadLeft(2, '0')) | string.IsNullOrEmpty(h.DBYF)) & h.BBlxdm == "1" & (string.IsNullOrEmpty(h.QYDL_DM) | ((h.QYDL_DM == infoModel.QYDL_DM & string.IsNullOrEmpty(h.Qylx_dm)) | (!string.IsNullOrEmpty(h.QYDL_DM) & !string.IsNullOrEmpty(h.Qylx_dm) & h.QYDL_DM == infoModel.QYDL_DM & h.Qylx_dm == infoModel.QYLX_DM)))).GroupBy(h => new { BBXM_DM = h.BBXM_DM, BBXM_MC = h.BBXM_MC }).Select(h => new Model.DM_QYBB { BBXM_DM = h.Key.BBXM_DM, BBXM_MC = h.Key.BBXM_MC }).ToList();

            List<Model.DM_QYBB> JBQYBBList = DMQYBBList.Where(h => (h.DBYF.Contains(LastDt.Month.ToString().PadLeft(2, '0')) | string.IsNullOrEmpty(h.DBYF)) & h.BBlxdm == "2" & (string.IsNullOrEmpty(h.QYDL_DM) | ((h.QYDL_DM == infoModel.QYDL_DM & string.IsNullOrEmpty(h.Qylx_dm)) | (!string.IsNullOrEmpty(h.QYDL_DM) & !string.IsNullOrEmpty(h.Qylx_dm) & h.QYDL_DM == infoModel.QYDL_DM & h.Qylx_dm == infoModel.QYLX_DM)))).GroupBy(h => new { BBXM_DM = h.BBXM_DM, BBXM_MC = h.BBXM_MC }).Select(h => new Model.DM_QYBB { BBXM_DM = h.Key.BBXM_DM, BBXM_MC = h.Key.BBXM_MC }).ToList();


            List<Model.DM_QYBB> NBQYBBList = DMQYBBList.Where(h => (h.DBYF.Contains(LastDt.Month.ToString().PadLeft(2, '0')) | string.IsNullOrEmpty(h.DBYF)) & h.BBlxdm == "3" & (string.IsNullOrEmpty(h.QYDL_DM) | ((h.QYDL_DM == infoModel.QYDL_DM & string.IsNullOrEmpty(h.Qylx_dm)) | (!string.IsNullOrEmpty(h.QYDL_DM) & !string.IsNullOrEmpty(h.Qylx_dm) & h.QYDL_DM == infoModel.QYDL_DM & h.Qylx_dm == infoModel.QYLX_DM)))).GroupBy(h => new { BBXM_DM = h.BBXM_DM, BBXM_MC = h.BBXM_MC }).Select(h => new Model.DM_QYBB { BBXM_DM = h.Key.BBXM_DM, BBXM_MC = h.Key.BBXM_MC }).ToList();

            List<Model.DM_QYBB> FJQYBBList = DMQYBBList.Where(h => (h.DBYF.Contains(LastDt.Month.ToString().PadLeft(2, '0')) | string.IsNullOrEmpty(h.DBYF)) & h.BBlxdm == "4" & (string.IsNullOrEmpty(h.QYDL_DM) | ((h.QYDL_DM == infoModel.QYDL_DM & string.IsNullOrEmpty(h.Qylx_dm)) | (!string.IsNullOrEmpty(h.QYDL_DM) & !string.IsNullOrEmpty(h.Qylx_dm) & h.QYDL_DM == infoModel.QYDL_DM & h.Qylx_dm == infoModel.QYLX_DM)))).GroupBy(h => new { BBXM_DM = h.BBXM_DM, BBXM_MC = h.BBXM_MC }).Select(h => new Model.DM_QYBB { BBXM_DM = h.Key.BBXM_DM, BBXM_MC = h.Key.BBXM_MC }).ToList();

            List<Model.DM_QYBB> DMQYBBListNew = new List<Model.DM_QYBB>();
            DMQYBBListNew.AddRange(YBQYBBList);
            DMQYBBListNew.AddRange(JBQYBBList);
            DMQYBBListNew.AddRange(NBQYBBList);
            DMQYBBListNew.AddRange(FJQYBBList);

            if (DMQYBBListNew.Count > 0)
            {
                foreach (Model.DM_QYBB item in DMQYBBListNew)
                {
                    string BQS = Fetch.Post(item.BBXM_DM + "BQS");
                    decimal BQSout;

                    Model.SB_QYBB SBQYBBModel = new Model.SB_QYBB();
                    if (SBQYBBListOld.Where(h => h.BBXM_DM.Trim() == item.BBXM_DM.Trim()).Count() > 0)
                    {

                        SBQYBBModel = SBQYBBListOld.FirstOrDefault(h => h.BBXM_DM.Trim() == item.BBXM_DM.Trim() & h.LC == "1");
                    }
                    else
                    {
                        SBQYBBModel.ZTDM = "1";

                    }
                    SBQYBBModel.BBXM_DM = item.BBXM_DM;
                    SBQYBBModel.QYBM = LoginID;
                    if (string.IsNullOrEmpty(BQS))
                    {
                        SBQYBBModel.JE = null;
                    }
                    else
                    {

                        if (decimal.TryParse(BQS, out BQSout))
                        {
                            SBQYBBModel.JE = BQSout;
                        }
                        else
                        {
                            isSuccess = false;
                            errorMessage = item.BBXM_MC + "本期数填写错误";

                        }
                    }
                    SBQYBBModel.Lrrq = DateTime.Now;
                    SBQYBBModel.LC = "1";
                    SBQYBBModel.SSSQ_Q = DateTime.Parse(GetStartAndEndTime(LastDt, item.BBlxdm).Split('%')[0]);
                    SBQYBBModel.SSSQ_Z = DateTime.Parse(GetStartAndEndTime(LastDt, item.BBlxdm).Split('%')[1]);
                    SBQYBBModel.SSND = LastDt.Year.ToString();
                    SBQYBBModel.SSYF = LastDt.Month.ToString().PadLeft(2, '0');
                    SBQYBBList.Add(SBQYBBModel);



                }


            }
            else
            {

                isSuccess = false;
                errorMessage = "当前月份无需填写报表";
            }

            if (isSuccess)
            {
                switch (Fetch.Post("hdMethod"))
                {


                    case BaseStruct.ActionOperationStruct.ADD_NEXT:
                        {

                            SBQYBBBLL.AddList(SBQYBBList);

                            errorMessage = "操作成功";

                            break;
                        }


                    case BaseStruct.ActionOperationStruct.EDIT:
                        {
                            SBQYBBBLL.EditList(SBQYBBList.Where(h => h.ID == 0).ToList(), SBQYBBList.Where(h => h.ID != 0).ToList()); ;

                            errorMessage = "操作成功";
                            break;
                        }

                }

            }
            return errorMessage;
        }

        public string GetStartAndEndTime(DateTime Model, string lx)
        {
            switch (lx)
            {


                case "1":
                    {


                        return (new DateTime(Model.Year, Model.Month, 1).ToString("yyyy-MM-dd") + "%" + new DateTime(Model.Year, Model.Month, 1).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd"));
                        break;
                    }
                case "2":
                    {


                        if (Model.Month >= 3 & Model.Month < 6)
                        {

                            return (new DateTime(Model.Year, 1, 1).ToString("yyyy-MM-dd") + "%" + new DateTime(Model.Year, 4, 1).AddDays(-1).ToString("yyyy-MM-dd"));

                        }
                        else if (Model.Month >= 6 & Model.Month < 9)
                        {

                            return (new DateTime(Model.Year, 4, 1).ToString("yyyy-MM-dd") + "%" + new DateTime(Model.Year, 7, 1).AddDays(-1).ToString("yyyy-MM-dd"));
                        }
                        else if (Model.Month >= 9 & Model.Month < 12)
                        {

                            return (new DateTime(Model.Year, 7, 1).ToString("yyyy-MM-dd") + "%" + new DateTime(Model.Year, 10, 1).AddDays(-1).ToString("yyyy-MM-dd"));


                        }
                        else if (Model.Month == 12)
                        {
                            return (new DateTime(Model.Year, 10, 1).ToString("yyyy-MM-dd") + "%" + new DateTime(Model.Year + 1, 1, 1).AddDays(-1).ToString("yyyy-MM-dd"));

                        }
                        else
                        {

                            return (new DateTime(Model.Year - 1, 10, 1).ToString("yyyy-MM-dd") + "%" + new DateTime(Model.Year, 1, 1).AddDays(-1).ToString("yyyy-MM-dd"));
                        }



                        break;
                    }

                case "3":
                    {
                        if (Model.Month >= 6 & Model.Month <= 12)
                        {

                            return (new DateTime(Model.Year, 1, 1).ToString("yyyy-MM-dd") + "%" + new DateTime(Model.Year + 1, 1, 1).AddDays(-1).ToString("yyyy-MM-dd"));

                        }
                        else
                        {

                            return (new DateTime(Model.Year - 1, 1, 1).ToString("yyyy-MM-dd") + "%" + new DateTime(Model.Year, 1, 1).AddDays(-1).ToString("yyyy-MM-dd"));

                        }
                        break;

                    }

                case "4":
                    {
                        return (new DateTime(Model.Year, Model.Month, 1).ToString("yyyy-MM-dd") + "%" + new DateTime(Model.Year, Model.Month, 1).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd"));

                        break;
                    }
                default:
                    {

                        return (new DateTime(Model.Year, Model.Month, 1).ToString("yyyy-MM-dd") + "%" + new DateTime(Model.Year, Model.Month, 1).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd"));

                        break;
                    }
            }

        }
    }
}
